package AnalysisApi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class FileUtils {

	public static byte[] multipartFileToByte(long fileSize,InputStream InputStream){
		byte[] b = null;
		InputStream is = InputStream;
		try{
			b = new byte[(int) fileSize];
			int read = 0;
			int i = 0;
			while ((read = InputStream.read()) != -1) {
				b[i] = (byte) read;
				i++;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return b;
	}
	
	public ArrayList<String> readFromTextFile(String pathname) throws IOException{
	    ArrayList<String> strArray = new ArrayList<String>();
	    File filename = new File(pathname);
	    InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));
	    BufferedReader br = new BufferedReader(reader);
	    String line = "";
	    line = br.readLine();
	    while(line != null) {
	        strArray.add(line);
	        line = br.readLine();
	    }
	    return strArray;
	}
	
		public StringBuffer readFile(String filepath) throws IOException {
//		            BufferedWriter out = new BufferedWriter(new FileWriter(filepath));
//		            out.write("菜鸟教程");
//		            out.append("dasd");
//		            out.newLine();
//		            out.write("你好");
//		            out.flush();
//		            out.close();
//		            System.out.println("文件创建成功！");
		            BufferedReader in = new BufferedReader(new FileReader(filepath));
		            StringBuffer sb = null;
		            while (in.ready()) {
		                sb = (new StringBuffer(in.readLine()));
		                System.out.println(sb);
		            }
		            in.close();
		            return sb;
		}
}
